package cn.kooki.app.duobao.ui.Fragment.share;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseFragment;
import cn.kooki.app.duobao.base.BaseRecyclerAdapter;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.Bean.Index.IndexGoodResponse;
import cn.kooki.app.duobao.data.Bean.share.ShareListItem;
import cn.kooki.app.duobao.data.Bean.share.ShareResponse;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Goods.GoodsDetailAcitvity;
import cn.kooki.app.duobao.ui.Activity.Share.ShareDetailActivity;
import cn.kooki.app.duobao.ui.Adapter.ListRecommAdapter;
import cn.kooki.app.duobao.ui.Adapter.ShareAdapter;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.view.LoadMoreContainer;
import cn.kooki.app.duobao.ui.view.LoadMoreHandler;
import cn.kooki.app.duobao.ui.view.LoadMoreListViewContainer;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import de.greenrobot.event.EventBus;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/18.
 */
public class UserCenterShareFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.share_title)
    TextView shareTitle;
    @Bind(R.id.blank_go_duobao)
    Button blankGoDuobao;
    @Bind(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @Bind(R.id.list_recommend)
    RecyclerView listRecommend;
    @Bind(R.id.emptyWrapper)
    ScrollView emptyWrapper;
    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.load_more_content_container)
    LoadMoreListViewContainer loadMoreContentContainer;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loading_wrapper)
    CustomRelativeLayout loadingWrapper;
    private ListRecommAdapter listRecommAdapter;
    private ArrayList<ShareListItem> datas = new ArrayList<>();
    private ShareAdapter shareAdapter;
    private int page = 1;
    private String uid;

    @Override
    public View setView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.blank_share_my, container, false);
        return view;
    }

    @Override
    public void afterView() {
        listRecommend.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        listRecommAdapter = new ListRecommAdapter(context);
        listRecommend.setAdapter(listRecommAdapter);
        listRecommAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                GoodsItem item = (GoodsItem) data;
                Intent intent = new Intent(context, GoodsDetailAcitvity.class);
                intent.putExtra("id", item.getId());
                intent.putExtra("qishu", item.getQishu());
                context.startActivity(intent);
            }
        });
        gettuijian();
        loadingWrapper.showloading();
        emptyWrapper.setVisibility(View.GONE);
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        uid = getArguments().getString("uid");
        shareAdapter = new ShareAdapter(context, datas);
        listView.setAdapter(shareAdapter);
        blankGoDuobao.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShareListItem item = datas.get(position);
                intent(ShareDetailActivity.class, "share_item", item);
            }
        });
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        CustomRefreshHeader header = new CustomRefreshHeader(context);
        swipeRefreshLayout.addPtrUIHandler(header);
        swipeRefreshLayout.setHeaderView(header);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }
        });
        loadMoreContentContainer.useDefaultFooter();

        loadMoreContentContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                getdata(true);
            }
        });

        listRecommAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                GoodsItem item = (GoodsItem) data;
                Intent intent = new Intent(context, GoodsDetailAcitvity.class);
                intent.putExtra("id", item.getId());
                intent.putExtra("qishu", item.getQishu());
                context.startActivity(intent);
            }
        });
        getdata(false);
    }


    private void getdata(final boolean isloadmore) {
        if (!isloadmore) {
            page = 1;
        } else {
            page++;
        }
        MainFactory.getApiInstance().listShare(uid, page, new Callback<ShareResponse>() {
            @Override
            public void success(ShareResponse baseResponse, Response response) {
                hideloading();
                if (baseResponse.getData() != null) {
                    if (!isloadmore) {
                        datas.clear();
                    }
                    datas.addAll(baseResponse.getData());
                    shareAdapter.notifyDataSetChanged();
                    switchloading();

                    loadMoreContentContainer.loadMoreFinish(datas.size() == 0, baseResponse.getData().size() == Constants.DEFAULT_COUNT);
                } else {
                    switchloading();
                }
            }

            @Override
            public void failure(RetrofitError error) {
                hideloading();
                switchloading();
            }
        });

    }

    public void switchloading() {
        if (datas.size() == 0) {
            emptyWrapper.setVisibility(View.VISIBLE);
            loadingWrapper.setVisibility(View.GONE);
        } else {
            emptyWrapper.setVisibility(View.GONE);
            loadingWrapper.setVisibility(View.VISIBLE);
        }
    }


    private void hideloading() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.refreshComplete();
            loadingWrapper.hideloading();
        }
    }

    /**
     * 获取推荐商品
     */
    private void gettuijian() {
        MainFactory.getApiInstance().tuijianlist(new Callback<IndexGoodResponse>() {
            @Override
            public void success(IndexGoodResponse indexGoodResponse, Response response) {
                if (indexGoodResponse.getData() != null) {
                    listRecommAdapter.mDatas.clear();
                    listRecommAdapter.addDatas(indexGoodResponse.getData());
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        if (v == blankGoDuobao) {
            ChangePageEvent pageEvent = new ChangePageEvent();
            pageEvent.item = 0;
            EventBus.getDefault().post(pageEvent);
        }
    }
}
